﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DacSanMienTayOBJ
{
    public class GroupProductBusiness
    {
        public static bool InsertGroupProduct(GroupProduct group, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                bool rs = false;
                string sql = "insert into groupproduct(GroupName,Metakeyword,IsVisible,Priority) values(@groupName,@metakey,@isVisible,@priority)";
                SqlParameter[] pa = new SqlParameter[4];
                pa[0] = new SqlParameter("@groupName", group.GroupName);
                pa[1] = new SqlParameter("@metakey",group.MetaKeyword);
                pa[2] = new SqlParameter("@isVisible",group.IsVisible);
                pa[3] = new SqlParameter("@priority",group.Priority);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateGroupProduct(int groupID,GroupProduct group, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                bool rs = false;
                string sql = "update groupproduct set GroupName=@groupName,Metakeyword=@metakey where GroupID=@groupID";
                SqlParameter[] pa = new SqlParameter[3];
                pa[0] = new SqlParameter("@groupName", group.GroupName);
                pa[1] = new SqlParameter("@metakey", group.MetaKeyword);
                pa[2] = new SqlParameter("@groupID", groupID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }
        public static bool UpdateGroupProductVisible(int groupID, int visible, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(ConnectionType.AdminConnection);
            try
            {
                bool rs = false;
                string sql = "update groupproduct set IsVisible=@isVisible where GroupID=@groupID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@isVisible", visible);
                pa[1] = new SqlParameter("@groupID", groupID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<GroupProduct> ListAllGroupProduct(ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<GroupProduct> list = new List<GroupProduct>();
                string sql = "select *from groupproduct";
                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    GroupProduct group = new GroupProduct();
                    group.GroupID = int.Parse(reader["GroupID"].ToString());
                    group.GroupName = reader["GroupName"].ToString();
                    group.MetaKeyword = reader["Metakeyword"].ToString();
                    group.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    group.Priority = int.Parse(reader["Priority"].ToString());
                    list.Add(group);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<GroupProduct> ListAllGroupProduct(int visible,ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<GroupProduct> list = new List<GroupProduct>();
                string sql = "select *from groupproduct where IsVisible=@isVisible";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@isVisible",visible);
                SqlCommand command = helper.GetCommand(sql,pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    GroupProduct group = new GroupProduct();
                    group.GroupID = int.Parse(reader["GroupID"].ToString());
                    group.GroupName = reader["GroupName"].ToString();
                    group.MetaKeyword = reader["Metakeyword"].ToString();
                    group.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    group.Priority = int.Parse(reader["Priority"].ToString());
                    list.Add(group);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static GroupProduct GetGroupProductDetail(int groupID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                GroupProduct group = null;
                string sql = "select *from groupproduct where GroupID=@groupID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@groupID", groupID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    group = new GroupProduct();
                    group.GroupID = int.Parse(reader["GroupID"].ToString());
                    group.GroupName = reader["GroupName"].ToString();
                    group.MetaKeyword = reader["Metakeyword"].ToString();
                    group.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    group.Priority = int.Parse(reader["Priority"].ToString());
                    
                }
                return group;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }
    }
}
